//
// Created by Administrator on 2022/1/9 0009.
//
#include <iostream>
#include "t_func_template.h"

//测试函数模板
void test_func_template(){

    using namespace std;
    int a = 10,b = 20;
    swap_t<>(a,b);//隐式实例化

    cout<<"new a:"<<a<<" new b:"<<b<<endl;

    double e = 100,f = 200;
    swap_t<double>(e,f);//显示实例化
    cout<<"new e:"<<e<<" new f:"<<f<<endl;

//    数组
    int arr1[8]{0,7,0,4,1,7,7,6};
    int arr2[8]{0,7,2,0,1,9,6,9};
    swap_tarr<>(arr1,arr2,8);//隐式实例化 //swap_tarr(arr1,arr2,8);
    show_tarr(arr1,8);
    show_tarr(arr2,8);

//    模板具体化
    job job_hsj{"hsj",10000.2,10};
    job job_hsx{"hsx",10001.2,11};
    swap_t<job>(job_hsj,job_hsx);//显式具体化
    show_job(job_hsj);
    show_job(job_hsx);
}

template <typename T> //or class T
void swap_t(T &a,T &b){
    T temp;
    temp = a;
    a = b;
    b = temp;
}

//显示具体化模板,也就是对模板函数swap_t的具体化
//template <typename T> //or class T
//void swap_t(T &a,T &b)
template<> void swap_t<job>(job &a,job &b){

    double salary_temp;
    int floor_temp;
    salary_temp = a.salary;
    a.salary = b.salary;
    b.salary = salary_temp;

    floor_temp = a.floor;
    a.floor = b.floor;
    b.floor = floor_temp;

};
//显示实例化
//template void swap_t<double>(double &a,double &b);

template <class T> //or typename T
void swap_tarr(T *a,T*b,int n ){

    T temp;
    for (int i = 0; i < n; ++i) {
        temp = a[i];
        a[i] = b[i];
        b[i] = temp;
    }
}



//展示数组元素
void show_tarr(int a[],int n){
    using namespace std;
    if (a == NULL){
        return;
    }
    for (int i = 0; i < n; ++i) {
        cout<<a[i]<<"\t";
    }
    cout<<endl;
}

//展示
void show_job(job &j){
    using namespace std;
    cout<<"name:"<<j.name<<"\t"<<"floor:"<<j.floor<<"\t"<<"salary:"<<j.salary<<endl;
}